package top.lfyao.designpattern.details.prototype;

import java.io.*;

/**
 * 简历类
 */
public class Resume implements Cloneable, Serializable {

    private static final long serialVersionUID = 4855003481709022972L;
    private String name;
    private String gender;
    private int age;

    // 引用“工作经历”对象，为演示深度拷贝和浅度拷贝而用
    private WorkExperience workExperience;

    public void display() {
        System.out.println(this.name+" "+this.gender+" "+this.age
                +"\n工作经历："
                +this.workExperience.getWorkeDate()+" "
                +this.workExperience.getWorkCompany()
        );
    }


    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    // 通过对象序列化，实现深度拷贝
    public Object deepClone() throws IOException, ClassNotFoundException {
        // 将对象写入流内
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(this);

        // 从流内读出对象
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
        return ois.readObject();
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public WorkExperience getWorkExperience() {
        return workExperience;
    }

    public void setWorkExperience(WorkExperience workExperience) {
        this.workExperience = workExperience;
    }
}
